package com.quantstack.llcommonlibrary.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;

import com.quantstack.llcommonlibrary.R;
import com.quantstack.llcommonlibrary.event.OnClickEvent;
import com.quantstack.llcommonlibrary.manager.DialogManager;
import com.quantstack.llcommonlibrary.manager.DialogView;

import java.util.Date;

/**
 * @author wu_ming_zhi_bei
 * @date 2021/2/26 10:15
 * @Notes
 */
public class DateDialog {

    private  DatePicker datePicker;
    /**
     * 日期弹窗
     * @param context
     * @param title
     * @param cancel
     * @param ok
     * @return
     */
    public  DialogView getDateDialog(Context context,String title,String cancel,String ok){
        DialogView dialogView = DialogManager.getInstance().initView(context, R.layout.layout_date, Gravity.BOTTOM);
        TextView tvTitle = dialogView.findViewById(R.id.tv_title);
        TextView tvCancel = dialogView.findViewById(R.id.tv_cancel);
        tvCancel.setText(cancel);
        TextView tvOk = dialogView.findViewById(R.id.tv_ok);
        tvOk.setText(ok);
        datePicker = dialogView.findViewById(R.id.date_picker);
        datePicker.setMaxDate(new Date().getTime());//设置日期的上限日期
        tvTitle.setText(title);
        tvCancel.setOnClickListener(new OnClickEvent() {
            @Override
            public void singleClick(View v) {
                if(onDateDialogListener!=null){
                    onDateDialogListener.onCancel();
                }
            }
        });

        tvOk.setOnClickListener(new OnClickEvent() {
            @Override
            public void singleClick(View v) {
                if(onDateDialogListener!=null){
                    int month = datePicker.getMonth()+1;
                    int day = datePicker.getDayOfMonth();
                    String m = month+"";
                    String d = day+"";
                    if(month<10){
                        m = "0"+month;
                    }
                    if(day<10){
                        d = "0"+day;
                    }
                    String date = d+"/"+m+"/"+datePicker.getYear();
                    onDateDialogListener.onOk(date);
                }
            }
        });
        return dialogView;
    }

    private OnDateDialogListener onDateDialogListener;

    public void setOnDateDialogListener(OnDateDialogListener onDateDialogListener) {
        this.onDateDialogListener = onDateDialogListener;
    }

    public interface OnDateDialogListener{
        void onOk(String date);
        void onCancel();
    }
}
